package com.nlscan.qdkit.ui.publico.util;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import java.io.Serializable;

public class HandlerUtils {

    public static final String KEY_RESULT = "result";

    public static void sendMsg(Handler handler, int what, Object... list) {
        // 发消息
        Message msg = Message.obtain();
        if (list != null && list.length > 0) {
            Bundle bundle = new Bundle();
            bundle.putSerializable(KEY_RESULT, (Serializable) list[0]);
            msg.setData(bundle);
            msg.what = what;
            handler.sendMessage(msg);
        } else {
            handler.sendEmptyMessage(what);
        }
    }

    /**
     * 发送延迟消息
     */
    public static void sendDelayMsg(Handler handler, int what, int delayMillis, Object... list) {
        // 发消息
        Message msg = Message.obtain();
        if (list != null && list.length > 0) {
            Bundle bundle = new Bundle();
            bundle.putSerializable(KEY_RESULT, (Serializable) list[0]);
            msg.setData(bundle);
            msg.what = what;
            handler.sendMessageDelayed(msg, delayMillis);
        } else {
            handler.sendEmptyMessageDelayed(what, delayMillis);
        }
    }
}
